<!DOCTYPE html>
<html>
<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">


    <title>聊天室demo</title>
    <meta name="keywords" content="">
    <meta name="description" content="">

    <link href="css/bootstrap.min14ed.css?v=3.3.6" rel="stylesheet">



    <link href="css/style.min862f.css?v=4.1.0" rel="stylesheet">
    <script>if(window.top !== window.self){ window.top.location = window.location;}</script>

</head>

<body class="gray-bg">

<div class="middle-box text-center loginscreen   animated fadeInDown">
    <div>
        <div>

            <h1 class="logo-name">AT</h1>

        </div>
        <h3>欢迎注册</h3>
        <p>创建一个demo帐户</p>
        <form class="m-t" role="form" action="" method='post' id="formR">
            <div class="form-group">
                <input type="text" class="form-control" placeholder="请输入用户名" name="UserName" required="">
            </div>
            <div class="form-group">
                <input type="password" class="form-control" placeholder="请输入密码" name="Password" required="">
            </div>
            <input type='button' id="register" value='注 册' class="btn btn-primary block full-width m-b" onclick="register()">
            <p class="text-muted text-center"><small>已经有账户了？</small><a href="http://localhost:1999/login.html">点此登录</a>
            </p>

        </form>
    </div>
</div>
<script src="js/jquery.min.js?v=2.1.4"></script>
<script src="js/bootstrap.min.js?v=3.3.6"></script>
<script type="text/javascript">


    $(document).ready(function()
    {
        $('#register').click(function(e)
        {
            var formObject = {};
            var array = $("#formR").serializeArray();
            $.each(array,function(i,item){
                formObject[item.name] = item.value;
            })
            $.ajax({
                // 几个参数需要注意一下
                type: "POST",//方法类型
                dataType: "json",//预期服务器返回的数据类型
                url: "http://localhost:8080/v1/user/register" ,//url
                data:   JSON.stringify(formObject) ,
                success: function (result) {
                    if (result.code == 0) {
                        login(JSON.stringify(formObject) )

                    }else  {
                        alert(result.msg)
                    }

                },
                error : function() {
                    alert("异常！");
                }
            });
        });

    });
    function login(jsonData) {
        $.ajax({
            //几个参数需要注意一下
            type: "POST",//方法类型
            dataType: "json",//预期服务器返回的数据类型
            url: "http://localhost:8080/v1/user/login" ,//url
            data:   jsonData ,
            success: function (result) {
                if (result.code == 0) {
                    setCookie("auth", result.data.Auth)
                    window.location.href="http://localhost:1999/";

                }else{
                    alert(result.msg)
                }
            },
            error : function() {
                alert("异常！");
            }
        });
    }
    function setCookie(name,value)
    {
        localStorage.setItem(name, value)
    }


</script>



</body>
</html>
